<template>
  <div class="login-wrapper">
    <div class="login-main">
      <h3 class="login-title">电影票出售管理系统</h3>

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>
        <el-form-item prop="captcha">
          <el-input v-model="ruleForm.captcha" placeholder="验证码"></el-input>
        </el-form-item>
        <el-image
              display:inline
              id="captcha-img"
              style="width: 130px; height: 48px"
              src="http://localhost:8082/admin/captcha"
              ></el-image>
              <el-button display:inline type="text" @click="changeImg()">不清楚，点击这里</el-button>
        <el-form-item>
          <el-button class="login-btn" type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>

    </div>
    <canvas id="canvas" class="login-anim"></canvas>
  </div>
</template>
<script type="text/javascript" src="/src/assets/js/jquery.js"></script>
<script type="text/javascript">
  import axios from 'axios';
  export default {
    name: 'login',
    data: function(){
      return {
        ruleForm: {
          username: 'admin',
          password: '123',
          captcha: ''
        },
        rules: {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'}
          ],
          captcha: [
            {required: true, message: '请输入验证码', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      changeImg() {
        console.log("changeImg")
              document.getElementById("captcha-img").src = "http://localhost:8082/admin/captcha?ran=" + Math.random();
      },
      submitForm(formName) {

        this.$refs[formName].validate(valid => {

          if (!valid) {

            this.$message.error('登录表单字段输入格式有误');
            return false;
          }
          //表单验证通过后，发送ajax请求，验证用户名密码是否正确
          console.log(this.ruleForm)

          axios.get('http://localhost:8082/admin/login',{params:this.ruleForm})
               .then(res =>{
                  if(res.data.code == 2000){//登录成功
                    //把用户名保存在 localStorage 里，localStorage里的数据各个页面、组件可以共享
                    // localStorage.setItem('user', res.data.data);
                    localStorage.setItem('username', res.data.admin.username);
                    localStorage.setItem('realName', res.data.admin.realName);
                      //跳转到首页,路由跳转
                      this.$router.push('/');
                  }else if(res.data.code == 4000){
                      this.$message.error(res.data.msg);
                  }else if(res.data.code == 4004){
                      this.$message.error(res.data.msg);
                      this.changeImg();
                  }else if(res.data.code == 4005){
                      this.$message.error(res.data.msg);
                  }else if(res.data.code == 5000){
                     this.$message.error('服务器内部错误，请稍后再试')
                  }
               })
        });
      }
    }
  }
</script>

<style scoped lang="less">
  
    // 改变input里的字体颜色
    /deep/input::-webkit-input-placeholder {
      color: #17a1e5;
      font-size: 15px;
    }

    // 改变input框背景颜色
    /deep/.el-input__inner {
      background-color: transparent !important;
      border: 1px solid #1296db;
    }

  .login-wrapper {
      width: 100%;
      height: 100%;
      min-width: 1000px;
      background-image: url("../../assets/images/login.jpg");
      background-size: 100% 100%;
      background-position: center center;
      overflow: auto;
      background-repeat: no-repeat;
      position: fixed;
      line-height: 100%;
      padding-top: 150px;
  }
  #canvas {
    width: 100%;
  }
  .login-main {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 350px;
    height: 450px;
    padding: 20px 35px;
    border: 1px solid #eee;
    margin: -255px 0 0 -160px;
    &::before, &::after {
      position: absolute;
      height: 12px;
      content: "";
    }
    &::before {
      left: 4px;
      right: 4px;
      top: -12px;
      z-index: 2;
      background-color: #f5f5f5;
    }
    &::after {
      left: 10px;
      right: 10px;
      top: -22px;
      z-index: 1;
      background-color: #f0f0f0;
    }
  }
  .login-title {
    padding-bottom: 15px;
    border-bottom: 2px solid @mainColor;
    margin: 15px 0 45px 0;
    color: #555;
    text-align: center;
    font-size: 30px;
  }
  .login-btn{
    width: 100%;
    height: 36px;
    margin-top: 30px;
    font-size: 16px;
  }
  .login-tip {
    color: #999;
    font-size: 12px;
    line-height: 30px;
  }
  .login-anim {
    position: absolute;
    bottom: 0;
    left: 0;
    z-index: -1;
  }
</style>
